package com.HMI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.Resources.CustomColor;

public class ScheduleRow {
	
	private MenuButton datePanel;
	private MenuButton timePanel;
	private String weekday;
	private String month;
	private String date;
	private String hour = "12";
	private String minute = "00";
	private String ampm = "PM";
	private ScheduleWindow parent;
	private ActionListener rowClicked;
	private Calendar calendar;
	private JPanel background;
	private boolean shockScheduled = false;
	
	public ScheduleRow(int y, int height, JPanel background, Calendar calendar, ScheduleWindow parent) {
		
		this.calendar = calendar;
		this.parent = parent;
		this.background = background;
		
		weekday = new SimpleDateFormat("EEE").format(calendar.getTime());
		month = new SimpleDateFormat("MMM").format(calendar.getTime());
		date = new SimpleDateFormat("d").format(calendar.getTime());

		createActionListener();
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d");
		
		datePanel = new MenuButton(dateFormat.format(calendar.getTime()), 77, y, 125, height);
		datePanel.setBackground(Color.WHITE);
		datePanel.setOpaque(true);
		datePanel.setForeground(CustomColor.NAVY_BLUE);
		datePanel.setHorizontalAlignment(SwingConstants.CENTER);
		datePanel.setVerticalAlignment(SwingConstants.CENTER);
		datePanel.setFont(new Font("DejaVu Sans", Font.PLAIN, 26));
		datePanel.setRolloverEnabled(false);
		datePanel.addActionListener(rowClicked);
		background.add(datePanel);
		
		String shockTime = parent.getShockTime(new SimpleDateFormat(
				"EEE, MMM, d").format(calendar.getTime()));
		
		timePanel = new MenuButton(shockTime, 203, y, 278, height);
		timePanel.setBackground(Color.WHITE);
		timePanel.setOpaque(true);
		timePanel.setForeground(CustomColor.NAVY_BLUE);
		timePanel.setHorizontalAlignment(SwingConstants.CENTER);
		timePanel.setVerticalAlignment(SwingConstants.CENTER);
		timePanel.setFont(new Font("DejaVu Sans", Font.PLAIN, 26));
		timePanel.setRolloverEnabled(false);
		timePanel.addActionListener(rowClicked);
		background.add(timePanel);
		
		if(!shockTime.equals("")) {
			String[] parts = shockTime.split("\\:"); // String array, each element is text between dots
			hour = parts[0];
			minute = parts[1].substring(0, 2);
			ampm = parts[1].substring(3);
			shockScheduled = true;
		}

	}
	
	private void createActionListener() {
		rowClicked = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				parent.showPopup(weekday, month, date, hour, minute, ampm, calendar, shockScheduled);
			}
			
		};
	}
	
	public void setBackground(Color color) {
		datePanel.setBackground(color);
		timePanel.setBackground(color);
	}
	
	public void removeRow() {
		background.remove(timePanel);
		background.remove(datePanel);
	}

}
